import { createRouter, createWebHashHistory, RouteRecordRaw } from 'vue-router'

const routes: Array<RouteRecordRaw> = [
  {
    path: '/home',
    meta: { title: '博客首页' },
    name: 'home',
    component: () =>
      import(/* webpackChunkName: "home" */ '@/views/BlogHome.vue'),
    children: [
      {
        path: 'main',
        name: 'main',
        meta: { title: '博客主页' },
        component: () =>
          import(/* webpackChunkName: "main" */ '@/views/home/BlogMain.vue'),
      },
      {
        path: 'classify',
        name: 'classify',
        meta: { title: '文章分类' },
        component: () =>
          import(
            /* webpackChunkName: "classify" */ '@/views/home/ArticleClassify.vue'
          ),
      },
      {
        path: 'search',
        name: 'search',
        meta: { title: '搜索文章' },
        component: () =>
          import(
            /* webpackChunkName: "search" */ '@/views/home/BlogSearch.vue'
          ),
      },
      {
        path: 'about',
        name: 'about',
        meta: { title: '关于' },
        component: () =>
          import(
            /* webpackChunkName: "about" */ '@/views/home/AboutCenter.vue'
          ),
      },
      {
        path: '/home',
        redirect: '/home/main',
      },
    ],
  },
  {
    path: '/login',
    name: 'login',
    meta: { title: '用户登陆' },
    component: () =>
      import(/* webpackChunkName: "login" */ '@/views/BlogLogin.vue'),
  },
  {
    path: '/talk',
    name: 'talk',
    meta: { title: '动态' },
    component: () =>
      import(/* webpackChunkName: "talk" */ '@/views/home/HomeTalk.vue'),
  },
  {
    path: '/message',
    name: 'message',
    meta: { title: '留言板' },
    component: () =>
      import(/* webpackChunkName: "message" */ '@/views/home/HomeMessage.vue'),
  },
  {
    path: '/details',
    name: 'details',
    meta: { title: '文章详情' },
    component: () =>
      import(/* webpackChunkName: "details" */ '@/views/ArticleDetails.vue'),
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/home',
  },
]

const router = createRouter({
  history: createWebHashHistory(process.env.BASE_URL),
  routes,
})
router.afterEach((to, from) => {
  // 每次切换的时候切换标题名字用到了路由元传参
  document.title = (to.meta.title as string) || '小赵博客'
})
export default router
